﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ComTools.Models;
using Dapper;

namespace ComTools
{
    public partial class frmCheckData : Form
    {
        public frmCheckData()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            //加载区域
            List<Location> locations = Reload<Location>();
            locations.Where(x => x.PATHLEVEL == 1);
            List<Trough> troughs = Reload<Trough>();
            List<Motor> motors = Reload<Motor>();
            List<NodeBasic> nodeBasics = Reload<NodeBasic>();
            //
            treeView1.Nodes.Clear();

        }

        private List<TreeNode> initTree(List<Location> locations, List<Trough> troughs, List<Motor> motors, List<NodeBasic> nodeBasics)
        {
            List<TreeNode> lists = new List<TreeNode>();
            //区域
            foreach (Location area in locations.Where(z => z.PID == 0 && z.PATHLEVEL == 1))
            {
                TreeNode tnArea = new TreeNode(area.NAME);
                tnArea.Tag = area;
                //线别
                foreach (Location line in locations.Where(y => y.PID == area.ID && y.PATHLEVEL == 2))
                {
                    TreeNode tnLine = new TreeNode(line.NAME);
                    tnLine.Tag = line;
                    //槽位
                    foreach (Trough trough in troughs.Where(x => x.LINEID == area.ID))
                    {
                        TreeNode tnTrough = new TreeNode(trough.TR_NAME);
                        tnTrough.Tag = trough;
                        //
                        tnLine.Nodes.Add(tnTrough);
                        //
                        foreach (Motor motor in motors.Where(w => w.TR_ID == trough.TR_ID))
                        {

                        }
                    }
                }
            }
            TreeNode node = new TreeNode();

            return lists;
        }

        private List<T> Reload<T>() where T : new()
        {
            T t = new T();
            String sql = "SELECT * FROM " + t.GetType().Name;
            IDbConnection _connection = OracleHelper.GetConnection();
            try
            {
                var r = _connection.Query<T>(sql);
                return r.ToList();
            }
            catch (Exception exc) { }
            return new List<T>();
        }

        private void BtnDataRecStatistic_Click(object sender, EventArgs e)
        {

        }

        private void BtnLoadEnv_Click(object sender, EventArgs e)
        {
            txtJavaHome.Text = Environment.GetEnvironmentVariable("JAVA_HOME");
        }

        private void BtnJavaVersion_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = UtilityHelper.RunDos("java -version");
            //Process p = new Process();
            //p.StartInfo = new ProcessStartInfo()
            //{
            //    FileName = "java.exe",
            //    Arguments = "version",
            //    CreateNoWindow = true,
            //    UseShellExecute = false,
            //    RedirectStandardInput = true,
            //    RedirectStandardOutput = true
            //};
            //p.Start();
            //richTextBox1.Text = p.StandardOutput.ReadToEnd();
            //p.WaitForExit(1000);
            //p.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool torf = UtilityHelper.IsValidIP(textBox1.Text);
            MessageBox.Show(torf.ToString());
        }
    }
}